package com.exit66.jukebox;

import org.mortbay.jetty.Server;
import org.mortbay.http.handler.ResourceHandler;
import org.mortbay.jetty.servlet.ServletHttpContext;

public class WebServer extends Thread {
    
    boolean _keepRunning = true;
    
    private static WebServer ref;
    protected Server _server;
    
	public static WebServer getWebServer()
    throws NullPointerException {
        if (ref == null)
            throw new NullPointerException();
        return ref;
    }
    
    public static WebServer getWebServer(int servPort, String httpRoot) {
        if (ref == null)
            ref = new WebServer(servPort, httpRoot);
        return ref;
    }
    
    public Object clone()
    throws CloneNotSupportedException {
        throw new CloneNotSupportedException();
    }
    
    
    private WebServer(int servPort, String httpRoot) {
    	try {
	    	setName("Exit66 Webserver");
	    	
			_server = new Server();
			_server.addListener(":" + servPort);

			ServletHttpContext context = (ServletHttpContext)_server.getContext("/");
			context.addServlet("/ws/autocomplete/*",
			                   "com.exit66.jukebox.servlet.AutoCompleteServlet");
			context.addServlet("/showimg/*",
			   				   "com.exit66.jukebox.servlet.AlbumCoverImageServlet");
			context.addServlet("/shutdown",
            				   "com.exit66.jukebox.servlet.ShutdownServlet");
			
			context.addServlet("/ws/albumlist/*",
            					"com.exit66.jukebox.servlet.xml.AlbumListServlet");
			context.addServlet("/ws/album/*",
								"com.exit66.jukebox.servlet.xml.AlbumListServlet");
			context.addServlet("/ws/artistlist/*",
								"com.exit66.jukebox.servlet.xml.ArtistListServlet");
			context.addServlet("/ws/artist/*",
								"com.exit66.jukebox.servlet.xml.ArtistListServlet");
			context.addServlet("/ws/currenttrack",
							   "com.exit66.jukebox.servlet.xml.CurrentTrackServlet");
			context.addServlet("/ws/queue",
							   "com.exit66.jukebox.servlet.xml.RequestQueueServlet");
			context.addServlet("/ws/tracklist/*",
							   "com.exit66.jukebox.servlet.xml.TrackListServlet");
			context.addServlet("/ws/track/*",
							   "com.exit66.jukebox.servlet.xml.TrackListServlet");
			context.addServlet("/ws/request/*",
			   				   "com.exit66.jukebox.servlet.xml.RequestServlet");
			context.addServlet("/ws/search/*",
			   				   "com.exit66.jukebox.servlet.xml.SearchServlet");
			
			context.addServlet("*.bvt",
			   				   "com.exit66.jukebox.servlet.BlueVadeTemplateServlet");
			
			context.setResourceBase(httpRoot);
			ResourceHandler handler = new ResourceHandler();
			handler.setDirAllowed(false);
			handler.setAcceptRanges(true);
			context.addHandler(handler);
			
			this.start();
    	}
    	catch (Exception e) {
    		System.err.println(e);
    	}
        
    }
    
    public void run() {
        
        try {
        	
        	_server.start();
            
            while (_keepRunning) {
                
                 Thread.sleep(1000);
                 
            }
            
            _server.stop();
            
        } catch (Exception e) { System.err.println(e); }
        System.out.println("Web server closed");
        
    }
    
    public synchronized void shutDown() {
        _keepRunning = false;
    }
}


